﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using ThreeByte.ActiveDeck.Config;
using System.Xml.Linq;
using System.Net;
using System.Xml;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Data;
using ThreeByte.ActiveDeck.Service;
using System.Threading;
using ThreeByte.ActiveDeck.Content;
using Microsoft.Office.Interop.PowerPoint;
using ThreeByte.ActiveDeck.AddIn.Config;
using ThreeByte.Media;
using ThreeByte.ActiveDeck.AddIn.Controls;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;


namespace ThreeByte.ActiveDeck.Controls
{
    /// <summary>
    /// Interaction logic for LayerChooserControl.xaml
    /// </summary>
    public partial class ColorChooserControl : UserControl, ICloseable, INotifyPropertyChanged
    {

        public event EventHandler<DialogResultEventArgs> Close;
        public event PropertyChangedEventHandler PropertyChanged;


        public ColorChooserControl() {
            InitializeComponent();

            Tag = "Choose New Color"; //Implemented to support customized title bar in Forms Host

            this.DataContext = this;
        }

        private void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e) {
            if(Close != null) {
                Close(this, new DialogResultEventArgs(System.Windows.Forms.DialogResult.Cancel));
            }
        }

        private Color _selectedColor;
        public Color SelectedColor {
            get {
                return _selectedColor;
            }
            set {
                Color oldColor = _selectedColor;
                _selectedColor = value;
                NotifyPropertyChanged("SelectedColor");

                if(_selectedColor.A != oldColor.A) {
                    NotifyPropertyChanged("SelectedAlpha");
                }
            }
        }

        //private byte _selectedOpacity;
        public byte SelectedAlpha {
            get {
                return _selectedColor.A;
            }
            set {
                if(SelectedAlpha != value) {
                    //Update Selected Color as well
                    SelectedColor = new Color() { A = value, R = SelectedColor.R, G = SelectedColor.G, B = SelectedColor.B };
                }
            }
        }

        private void DoneButton_Click(object sender, RoutedEventArgs e) {
            
            if(Close != null) {
                Close(this, new DialogResultEventArgs(System.Windows.Forms.DialogResult.OK));
            }
        }

        private void ColorSelect(object sender, MouseButtonEventArgs e) {
            Rectangle swatch = (Rectangle)sender;
            Color swatchColor = (Color)(swatch.Tag);

            Color newColor = new Color();
            newColor.A = SelectedColor.A;  //Preserve the Alpha
            newColor.R = swatchColor.R;
            newColor.G = swatchColor.G;
            newColor.B = swatchColor.B;

            SelectedColor = newColor;
        }

       

       
    }
}
